package org.his.test;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.google.gson.Gson;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class SendMessageUtils {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SendMessageUtils.class);

    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";

    // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找) 记录下来的
    static final String accessKeyId = "LTAIBWXoKcUiFPyV";
    static final String accessKeySecret = "tvp3eXs4CByC0kghez9dIlOcfhBaPx";

    /**
     * 发送短信
     * @param usrmap 短信模板变量对象
     * @return
     */
    public static  Map<String, Object>  sendMssage(Map<String, Object> usrmap){
        Map<String, Object> retmap = new HashMap<String, Object>();
        try{
            //解析传进的对象成json
            Gson gson = new Gson();

            //可自助调整超时时间
            System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
            System.setProperty("sun.net.client.defaultReadTimeout", "10000");

            //初始化acsClient,暂不支持region化
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);

            IAcsClient acsClient = new DefaultAcsClient(profile);

            //组装请求对象-具体描述见控制台-文档部分内容
            SendSmsRequest request = new SendSmsRequest();
            //必填:待发送手机号
            request.setPhoneNumbers(usrmap.get("phonenumber").toString());
            //必填:短信签名-可在短信控制台中找到
            request.setSignName("xxx");
            //必填:短信模板-可在短信控制台中找到
            request.setTemplateCode("xxx");
            //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
            //友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败
            request.setTemplateParam(gson.toJson(usrmap));
//            request.setTemplateParam("{\"code\":\"889977\"}");
            //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
            request.setOutId("yourOutId");

            //hint 此处可能会抛出异常，注意catch
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            System.err.println(sendSmsResponse.getCode());
            if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
                //请求成功
                logger.info("请求成功");
                retmap.put("result","发送成功");
                retmap.put("errorCode","200");

            }else{


                logger.info(sendSmsResponse.getCode());


                if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.OUT_OF_SERVICE")){

                    logger.info("业务停机");
                    retmap.put("result","业务停机,请登陆www.alidayu.com充值");
                    retmap.put("errorCode","400");
                }else if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.PRODUCT_UNSUBSCRIBE")){

                    logger.info("产品服务未开通");
                    retmap.put("result","产品服务未开通,请登陆www.alidayu.com开通相应的产品服务");
                    retmap.put("errorCode","400");

                }else if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.ACCOUNT_NOT_EXISTS")){

                    logger.info("账户信息不存在");
                    retmap.put("result","账户信息不存在,请登陆www.alidayu.com完成入驻");
                    retmap.put("errorCode","400");

                }else if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.ACCOUNT_ABNORMAL")){

                    logger.info("账户信息异常");
                    retmap.put("result","账户信息异常,联系技术支持");
                    retmap.put("errorCode","400");

                }else if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.SMS_TEMPLATE_ILLEGAL")){

                    logger.info("模板不合法");
                    retmap.put("result","模板不合法,请登陆www.alidayu.com查询审核通过短信模板使用");
                    retmap.put("errorCode","400");

                }else if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.SMS_SIGNATURE_ILLEGAL")){

                    logger.info("签名不合法");
                    retmap.put("result","签名不合法,请登陆www.alidayu.com查询审核通过的签名使用");
                    retmap.put("errorCode","400");

                }else if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.MOBILE_NUMBER_ILLEGAL")){

                    logger.info("手机号码格式错误");
                    retmap.put("result","手机号码格式错误,使用合法的手机号码");
                    retmap.put("errorCode","400");

                }else if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.MOBILE_COUNT_OVER_LIMIT")){

                    logger.info("手机号码数量超过限制");
                    retmap.put("result","手机号码数量超过限制,批量发送，手机号码以英文逗号分隔，不超过200个号码");
                    retmap.put("errorCode","400");

                }else if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.TEMPLATE_MISSING_PARAMETERS")){

                    logger.info("短信模板变量缺少参数");
                    retmap.put("result","短信模板变量缺少参数,确认短信模板中变量个数，变量名，检查传参是否遗漏");
                    retmap.put("errorCode","400");

                }else if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.INVALID_PARAMETERS")){

                    logger.info("参数异常");
                    retmap.put("result","参数异常,检查参数是否合法");
                    retmap.put("errorCode","400");

                }else if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.BUSINESS_LIMIT_CONTROL")){

                    logger.info("触发业务流控限制");
                    retmap.put("result","触发业务流控限制");
                    retmap.put("errorCode","400");

                }else if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.INVALID_JSON_PARAM")){

                    logger.info("JSON参数不合法");
                    retmap.put("result","参数不合法");
                    retmap.put("errorCode","400");

                }else if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.BLACK_KEY_CONTROL_LIMIT")){

                    logger.info("模板变量中存在黑名单关键字");
                    retmap.put("result","模板变量中存在黑名单关键字");
                    retmap.put("errorCode","400");

                }else if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.PARAM_NOT_SUPPORT_URL")){

                    logger.info("不支持url为变量");
                    retmap.put("result","不支持url为变量,域名和ip请固化到模板申请中");
                    retmap.put("errorCode","400");

                }else if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.PARAM_LENGTH_LIMIT")){

                    logger.info("变量长度受限");
                    retmap.put("result","变量长度受限");
                    retmap.put("errorCode","400");

                }else if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.AMOUNT_NOT_ENOUGH")){

                    logger.info("余额不足");
                    retmap.put("result","余额不足未能发送成功，请登录管理中心充值后重新发送");
                    retmap.put("errorCode","400");

                }else if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.DAY_LIMIT_CONTROL")){

                    logger.info("触发日发送限额");
                    retmap.put("result","触发日发送限额");
                    retmap.put("errorCode","400");

                }else if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.MONTH_LIMIT_CONTROL")){

                    logger.info("触发月发送限额");
                    retmap.put("result","触发月发送限额");
                    retmap.put("errorCode","400");

                }else if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.SMS_SIGN_ILLEGAL")){

                    logger.info("短信签名非法");
                    retmap.put("result","短信签名非法");
                    retmap.put("errorCode","400");

                }

            }
        } catch (ClientException e) {
            logger.info("阿里云短信接口异常",e);
            e.printStackTrace();
            retmap.put("result","阿里云短信接口异常");
            retmap.put("errorCode","405");
        }

        return retmap;
    }


    public static void main(String[] args) {
        Map<String, Object> smsModel = new HashMap<String, Object>();

        smsModel.put("code", "xx");
        smsModel.put("phonenumber", "xx");
        Map<String, Object> map= SendMessageUtils.sendMssage(smsModel);
        System.err.println(map);//返回参数
    }
}
